{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 观察Otto商品的特征进行PCA各维的方差，可以得到什么结论？（20分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:03.058669Z",
     "start_time": "2020-07-17T03:20:02.040644Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import numpy as np\n",
    "from sklearn.decomposition import PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:04.639800Z",
     "start_time": "2020-07-17T03:20:03.061662Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>feat_1</th>\n",
       "      <th>feat_2</th>\n",
       "      <th>feat_3</th>\n",
       "      <th>feat_4</th>\n",
       "      <th>feat_5</th>\n",
       "      <th>feat_6</th>\n",
       "      <th>feat_7</th>\n",
       "      <th>feat_8</th>\n",
       "      <th>feat_9</th>\n",
       "      <th>...</th>\n",
       "      <th>feat_85</th>\n",
       "      <th>feat_86</th>\n",
       "      <th>feat_87</th>\n",
       "      <th>feat_88</th>\n",
       "      <th>feat_89</th>\n",
       "      <th>feat_90</th>\n",
       "      <th>feat_91</th>\n",
       "      <th>feat_92</th>\n",
       "      <th>feat_93</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 95 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   id  feat_1  feat_2  feat_3  feat_4  feat_5  feat_6  feat_7  feat_8  feat_9  \\\n",
       "0   1       1       0       0       0       0       0       0       0       0   \n",
       "1   2       0       0       0       0       0       0       0       1       0   \n",
       "2   3       0       0       0       0       0       0       0       1       0   \n",
       "3   4       1       0       0       1       6       1       5       0       0   \n",
       "4   5       0       0       0       0       0       0       0       0       0   \n",
       "\n",
       "    ...     feat_85  feat_86  feat_87  feat_88  feat_89  feat_90  feat_91  \\\n",
       "0   ...           1        0        0        0        0        0        0   \n",
       "1   ...           0        0        0        0        0        0        0   \n",
       "2   ...           0        0        0        0        0        0        0   \n",
       "3   ...           0        1        2        0        0        0        0   \n",
       "4   ...           1        0        0        0        0        1        0   \n",
       "\n",
       "   feat_92  feat_93   target  \n",
       "0        0        0  Class_1  \n",
       "1        0        0  Class_1  \n",
       "2        0        0  Class_1  \n",
       "3        0        0  Class_1  \n",
       "4        0        0  Class_1  \n",
       "\n",
       "[5 rows x 95 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train = pd.read_csv(\"Otto_train.csv\")\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:04.681686Z",
     "start_time": "2020-07-17T03:20:04.643793Z"
    }
   },
   "outputs": [],
   "source": [
    "train_y = train['target']\n",
    "train_x = train.drop([\"id\", \"target\"], axis = 1)\n",
    "\n",
    "#用于储存PCA变换后的特征\n",
    "train_id = train['id']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:05.430845Z",
     "start_time": "2020-07-17T03:20:04.682683Z"
    }
   },
   "outputs": [],
   "source": [
    "#PCA降维\n",
    "\n",
    "#先生成一个PCA的实例，在调用fit函数，\n",
    "pca = PCA(n_components = 0.85)#取前85%的主成分\n",
    "pca.fit(train_x)\n",
    "\n",
    "# 在训练集降维\n",
    "pca_train_x = pca.transform(train_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:05.707904Z",
     "start_time": "2020-07-17T03:20:05.432839Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<BarContainer object of 34 artists>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#绘制PCA维的方差\n",
    "import matplotlib.pyplot as plt\n",
    "plt.bar(range(len(pca.explained_variance_ratio_)), pca.explained_variance_ratio_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如上图，横轴表示特征维度的索引，从图中可以看出一共保留了34个主成分，纵轴表示每一维的方差，在34维后的方差几乎都在0.15附近，所以降维后的34个主成分能解释的方差已经占到总体的85%，足可以代表原始数据所有的特征了。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 对Otto商品tfidf特征，进行PCA降维，给出各维方差的分布图。（30分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:09.147890Z",
     "start_time": "2020-07-17T03:20:05.708902Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>feat_1_tfidf</th>\n",
       "      <th>feat_2_tfidf</th>\n",
       "      <th>feat_3_tfidf</th>\n",
       "      <th>feat_4_tfidf</th>\n",
       "      <th>feat_5_tfidf</th>\n",
       "      <th>feat_6_tfidf</th>\n",
       "      <th>feat_7_tfidf</th>\n",
       "      <th>feat_8_tfidf</th>\n",
       "      <th>feat_9_tfidf</th>\n",
       "      <th>...</th>\n",
       "      <th>feat_85_tfidf</th>\n",
       "      <th>feat_86_tfidf</th>\n",
       "      <th>feat_87_tfidf</th>\n",
       "      <th>feat_88_tfidf</th>\n",
       "      <th>feat_89_tfidf</th>\n",
       "      <th>feat_90_tfidf</th>\n",
       "      <th>feat_91_tfidf</th>\n",
       "      <th>feat_92_tfidf</th>\n",
       "      <th>feat_93_tfidf</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.081393</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.075886</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.231403</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.199730</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>0.011987</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.011668</td>\n",
       "      <td>0.105971</td>\n",
       "      <td>0.021681</td>\n",
       "      <td>0.080435</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.008244</td>\n",
       "      <td>0.022456</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.124622</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.145988</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Class_1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 95 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   id  feat_1_tfidf  feat_2_tfidf  feat_3_tfidf  feat_4_tfidf  feat_5_tfidf  \\\n",
       "0   1      0.081393           0.0           0.0      0.000000      0.000000   \n",
       "1   2      0.000000           0.0           0.0      0.000000      0.000000   \n",
       "2   3      0.000000           0.0           0.0      0.000000      0.000000   \n",
       "3   4      0.011987           0.0           0.0      0.011668      0.105971   \n",
       "4   5      0.000000           0.0           0.0      0.000000      0.000000   \n",
       "\n",
       "   feat_6_tfidf  feat_7_tfidf  feat_8_tfidf  feat_9_tfidf   ...     \\\n",
       "0      0.000000      0.000000      0.000000           0.0   ...      \n",
       "1      0.000000      0.000000      0.231403           0.0   ...      \n",
       "2      0.000000      0.000000      0.199730           0.0   ...      \n",
       "3      0.021681      0.080435      0.000000           0.0   ...      \n",
       "4      0.000000      0.000000      0.000000           0.0   ...      \n",
       "\n",
       "   feat_85_tfidf  feat_86_tfidf  feat_87_tfidf  feat_88_tfidf  feat_89_tfidf  \\\n",
       "0       0.075886       0.000000       0.000000            0.0            0.0   \n",
       "1       0.000000       0.000000       0.000000            0.0            0.0   \n",
       "2       0.000000       0.000000       0.000000            0.0            0.0   \n",
       "3       0.000000       0.008244       0.022456            0.0            0.0   \n",
       "4       0.124622       0.000000       0.000000            0.0            0.0   \n",
       "\n",
       "   feat_90_tfidf  feat_91_tfidf  feat_92_tfidf  feat_93_tfidf   target  \n",
       "0       0.000000            0.0            0.0            0.0  Class_1  \n",
       "1       0.000000            0.0            0.0            0.0  Class_1  \n",
       "2       0.000000            0.0            0.0            0.0  Class_1  \n",
       "3       0.000000            0.0            0.0            0.0  Class_1  \n",
       "4       0.145988            0.0            0.0            0.0  Class_1  \n",
       "\n",
       "[5 rows x 95 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_tfidf = pd.read_csv(\"Otto_FE_train_tfidf.csv\")\n",
    "train_tfidf.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:09.187737Z",
     "start_time": "2020-07-17T03:20:09.148841Z"
    }
   },
   "outputs": [],
   "source": [
    "train_tfidf_y = train_tfidf['target']\n",
    "train_tfidf_x = train_tfidf.drop([\"id\", \"target\"], axis = 1)\n",
    "\n",
    "#用于储存PCA变换后的特征\n",
    "train_tfidf_id = train_tfidf['id']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:09.730440Z",
     "start_time": "2020-07-17T03:20:09.188734Z"
    }
   },
   "outputs": [],
   "source": [
    "#PCA降维\n",
    "\n",
    "#先生成一个PCA的实例，在调用fit函数，\n",
    "pca = PCA(n_components = 0.85)#取前85%的主成分\n",
    "pca.fit(train_tfidf_x)\n",
    "\n",
    "# 在训练集降维\n",
    "pca_train_tfidf_x = pca.transform(train_tfidf_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:09.896002Z",
     "start_time": "2020-07-17T03:20:09.732435Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<BarContainer object of 48 artists>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD8CAYAAACb4nSYAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAEA9JREFUeJzt3H+s3fVdx/Hny3bA3CJsUM1swXahmnUZTnfXLXHqMpQVmesSIZb5oyaYajLMjJuzGMe2OhMwZt0SMbEZaGVqIcwfN1IlBOaPLAvrZbgfHZLdIY5ryShrx8QFurK3f5wv2fFwy/3ee8/t5d7P85E0fL+f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LUkMMfUlqyP8Bst/13MP2gdUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.bar(range(len(pca.explained_variance_ratio_)), pca.explained_variance_ratio_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 采用train_test_split，从将数据集中随机抽取10000条记录（原始数据集太大，剩余数据抛弃，此部分SVM作业已经完成）。对这部分数据进行PCA降维，保留85%的能量。（20分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:16.599796Z",
     "start_time": "2020-07-17T03:20:09.897000Z"
    }
   },
   "outputs": [],
   "source": [
    "train1 = pd.read_csv(\"Otto_FE_train_org.csv\")\n",
    "train2 = pd.read_csv(\"Otto_FE_train_tfidf.csv\")\n",
    "\n",
    "train2 = train2.drop([\"id\",\"target\"], axis=1)\n",
    "train =  pd.concat([train1, train2], axis = 1, ignore_index=False)\n",
    "\n",
    "del train1\n",
    "del train2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:16.902682Z",
     "start_time": "2020-07-17T03:20:16.600759Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda3\\lib\\site-packages\\sklearn\\model_selection\\_split.py:2179: FutureWarning: From version 0.21, test_size will always complement train_size unless both are specified.\n",
      "  FutureWarning)\n"
     ]
    }
   ],
   "source": [
    "y_train = train['target'] \n",
    "x_train = train.drop([\"target\"], axis=1)\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "x_train_part, x_val, y_train_part, y_val = train_test_split(x_train, y_train, train_size = 10000, random_state = 0)#分为训练数据和测试数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:16.909658Z",
     "start_time": "2020-07-17T03:20:16.905676Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10000, 187)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train_part.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T04:30:51.324342Z",
     "start_time": "2020-07-17T04:30:51.319422Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(51878, 187)"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_val.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:16.933597Z",
     "start_time": "2020-07-17T03:20:16.916650Z"
    }
   },
   "outputs": [],
   "source": [
    "x_train_part_id = x_train_part[\"id\"]\n",
    "\n",
    "x_train_part = x_train_part.drop([\"id\"], axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:17.093241Z",
     "start_time": "2020-07-17T03:20:16.934592Z"
    }
   },
   "outputs": [],
   "source": [
    "pca = PCA(n_components = 0.85)\n",
    "pca.fit(x_train_part)\n",
    "    \n",
    "x_train_pca = pca.transform(x_train_part)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:17.100247Z",
     "start_time": "2020-07-17T03:20:17.095236Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10000, 53)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train_pca.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:17.278657Z",
     "start_time": "2020-07-17T03:20:17.102243Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "53\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<BarContainer object of 53 artists>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD8CAYAAACb4nSYAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAEItJREFUeJzt3X+s3Xddx/Hny5YNBLNBV4y2w1uyaiiKqLWQgIqbjM7hinGNHag1mZkmzGCAYGfigAoJM8b5hzOxcdMFxG0Zoo2rzoXhj5Bl9I4Bo4yGy5zsUsLubBlO3Ubh7R/nWz2c3XK/9/a0997zeT6S5n6/n+/ne87nk529vp/z+Z7zOakqJElt+I7lboAk6cwx9CWpIYa+JDXE0Jekhhj6ktQQQ1+SGmLoS1JDDH1JaoihL0kNWbvcDRh13nnn1dTU1HI3Q5JWlfvuu++xqlq/UL0VF/pTU1NMT08vdzMkaVVJ8u996jm9I0kNMfQlqSGGviQ1xNCXpIYY+pLUEENfkhpi6EtSQwx9SWqIoS9JDVlx38g9VVN77viW/Yffd+kytUSSVp5eI/0k25McTjKTZM88x38yySeSHE9y+cix3Uk+3/3bPa6GS5IWb8HQT7IGuAG4BNgCXJFky0i1LwK/Cnxw5NwXAO8EXgFsA96Z5Pmn3mxJ0lL0GelvA2aq6qGqehq4BdgxXKGqHq6qTwPfHDn3dcBdVXW0qo4BdwHbx9BuSdIS9An9DcAjQ/uzXVkfvc5NclWS6STTc3NzPR9akrRYfUI/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RbMmJVqqp/YfDJqGHDy1/cDLzhjDbqNKiqL1fVJ7rt/2QQFhuYoL7WwBPd7rO6fwVcyGAZE1jlfTwhyUbgUuDPuv0wgf08iRX5mp200G9t2YfvrqovwyAsgRcuc3vGqlut9UeAe5mwvnZTHp8EHgXuAr4AfLVbxgQm57X7R8A7gG92++uYzH4W8I9J7utWGIAV+pqdtB9GzzxlfjxpFUryPOBDwG9V1dcGA8TJ0X1f5eVJzgU+DLxkvmpntlXjleT1wKNVdV+S15wonqfqqu5n51VVdSTJC4G7knxuuRt0MpM20u+17MME+UqS7wHo/j66zO0ZiyTPYhD4f1lVf90VT2Rfq+qrwD8xuH9xbreMCUzGa/dVwGVJHmYw1Xohg5H/pPWTqjrS/X2UwUV8Gyv0NTtpod9nyYhJMrz8xW7gb5exLWPRzfneCDxYVX84dGhi+ppkfTfCJ8lzgJ9hcO/iowyWMYFV3keAqrqmqjZW1RSD/xfvrqo3MWH9TPLcJN91Yhu4GPgMK/Q1O3FfzkryswxGEyeWjFiObwOPXZK/Al7DYPW+rwDvBP4GuA14EfBFYGdVjd7sXVWSvBr4V+AB/n8e+HcYzOtPRF+TvIzBjb01DAZet1XV3iQvZjAifgFwP/BLVfXU8rV0fLrpnbdX1esnrZ9dfz7c7a4FPlhV702yjhX4mp240JckndykTe9Ikr4NQ1+SGmLoS1JDDH1JaoihL0kNMfQlqSGGviQ1xNCXpIb8L8amIpbDfbIeAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#绘制PCA维的方差\n",
    "print(len(pca.explained_variance_ratio_))\n",
    "plt.bar(range(len(pca.explained_variance_ratio_)), pca.explained_variance_ratio_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 对3中得到的数据（对降维后的数据），训练RBF核SVM，并对超参数（C和gamma）进行超参数调优。结果和用原始数据的情况比较（SVM部分作业结果）。（30分） "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:17.290625Z",
     "start_time": "2020-07-17T03:20:17.280652Z"
    }
   },
   "outputs": [],
   "source": [
    "#将降维后的特征进行组合\n",
    "n_components = pca.n_components_#保留的主成分的个数\n",
    "feat_names_pca = []\n",
    "for i in range(n_components):\n",
    "    feat_names_pca.append(\"pca_\" + str(i))\n",
    "    \n",
    "y = pd.Series(data = y_train_part.values, name = 'target')\n",
    "id_ = pd.Series(data = x_train_part_id.values, name = 'id')\n",
    "\n",
    "train_pca = pd.concat([id_, pd.DataFrame(columns = feat_names_pca, data = x_train_pca), y], axis = 1)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T03:20:17.295612Z",
     "start_time": "2020-07-17T03:20:17.291622Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10000, 55)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_pca.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T04:43:42.951436Z",
     "start_time": "2020-07-17T04:43:42.935465Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>pca_0</th>\n",
       "      <th>pca_1</th>\n",
       "      <th>pca_2</th>\n",
       "      <th>pca_3</th>\n",
       "      <th>pca_4</th>\n",
       "      <th>pca_5</th>\n",
       "      <th>pca_6</th>\n",
       "      <th>pca_7</th>\n",
       "      <th>pca_8</th>\n",
       "      <th>...</th>\n",
       "      <th>pca_44</th>\n",
       "      <th>pca_45</th>\n",
       "      <th>pca_46</th>\n",
       "      <th>pca_47</th>\n",
       "      <th>pca_48</th>\n",
       "      <th>pca_49</th>\n",
       "      <th>pca_50</th>\n",
       "      <th>pca_51</th>\n",
       "      <th>pca_52</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>7898</td>\n",
       "      <td>0.625286</td>\n",
       "      <td>0.131340</td>\n",
       "      <td>-0.002001</td>\n",
       "      <td>0.173749</td>\n",
       "      <td>0.004266</td>\n",
       "      <td>0.035673</td>\n",
       "      <td>0.201813</td>\n",
       "      <td>-0.035828</td>\n",
       "      <td>-0.104780</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.081093</td>\n",
       "      <td>-0.036612</td>\n",
       "      <td>-0.032685</td>\n",
       "      <td>0.026958</td>\n",
       "      <td>0.023477</td>\n",
       "      <td>-0.031667</td>\n",
       "      <td>-0.021681</td>\n",
       "      <td>0.014094</td>\n",
       "      <td>0.040825</td>\n",
       "      <td>Class_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11288</td>\n",
       "      <td>0.476990</td>\n",
       "      <td>0.013569</td>\n",
       "      <td>-0.290358</td>\n",
       "      <td>0.009830</td>\n",
       "      <td>0.053649</td>\n",
       "      <td>-0.085235</td>\n",
       "      <td>0.015529</td>\n",
       "      <td>-0.417157</td>\n",
       "      <td>-0.044583</td>\n",
       "      <td>...</td>\n",
       "      <td>0.008607</td>\n",
       "      <td>0.012202</td>\n",
       "      <td>-0.012626</td>\n",
       "      <td>-0.053581</td>\n",
       "      <td>0.087355</td>\n",
       "      <td>0.009804</td>\n",
       "      <td>-0.011665</td>\n",
       "      <td>-0.019390</td>\n",
       "      <td>0.047540</td>\n",
       "      <td>Class_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10356</td>\n",
       "      <td>0.142455</td>\n",
       "      <td>-0.238999</td>\n",
       "      <td>0.004232</td>\n",
       "      <td>-0.113067</td>\n",
       "      <td>-0.035355</td>\n",
       "      <td>-0.191490</td>\n",
       "      <td>-0.234224</td>\n",
       "      <td>-0.405027</td>\n",
       "      <td>0.267705</td>\n",
       "      <td>...</td>\n",
       "      <td>0.035690</td>\n",
       "      <td>-0.022022</td>\n",
       "      <td>-0.055516</td>\n",
       "      <td>-0.078802</td>\n",
       "      <td>-0.089903</td>\n",
       "      <td>0.081683</td>\n",
       "      <td>-0.128651</td>\n",
       "      <td>-0.144019</td>\n",
       "      <td>0.000786</td>\n",
       "      <td>Class_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13439</td>\n",
       "      <td>0.600814</td>\n",
       "      <td>0.076997</td>\n",
       "      <td>-0.239394</td>\n",
       "      <td>0.054835</td>\n",
       "      <td>0.007561</td>\n",
       "      <td>-0.088206</td>\n",
       "      <td>0.020324</td>\n",
       "      <td>-0.282483</td>\n",
       "      <td>-0.057967</td>\n",
       "      <td>...</td>\n",
       "      <td>0.123055</td>\n",
       "      <td>-0.091573</td>\n",
       "      <td>-0.005661</td>\n",
       "      <td>-0.027059</td>\n",
       "      <td>0.032352</td>\n",
       "      <td>0.083729</td>\n",
       "      <td>0.069337</td>\n",
       "      <td>0.025704</td>\n",
       "      <td>-0.095524</td>\n",
       "      <td>Class_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>54130</td>\n",
       "      <td>-0.133692</td>\n",
       "      <td>-0.167084</td>\n",
       "      <td>-0.076314</td>\n",
       "      <td>-0.079330</td>\n",
       "      <td>0.105393</td>\n",
       "      <td>-0.226724</td>\n",
       "      <td>0.056367</td>\n",
       "      <td>0.039180</td>\n",
       "      <td>-0.224581</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.030246</td>\n",
       "      <td>0.356076</td>\n",
       "      <td>0.198616</td>\n",
       "      <td>0.256928</td>\n",
       "      <td>-0.029483</td>\n",
       "      <td>-0.098445</td>\n",
       "      <td>-0.002309</td>\n",
       "      <td>0.016936</td>\n",
       "      <td>-0.085946</td>\n",
       "      <td>Class_8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 55 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      id     pca_0     pca_1     pca_2     pca_3     pca_4     pca_5  \\\n",
       "0   7898  0.625286  0.131340 -0.002001  0.173749  0.004266  0.035673   \n",
       "1  11288  0.476990  0.013569 -0.290358  0.009830  0.053649 -0.085235   \n",
       "2  10356  0.142455 -0.238999  0.004232 -0.113067 -0.035355 -0.191490   \n",
       "3  13439  0.600814  0.076997 -0.239394  0.054835  0.007561 -0.088206   \n",
       "4  54130 -0.133692 -0.167084 -0.076314 -0.079330  0.105393 -0.226724   \n",
       "\n",
       "      pca_6     pca_7     pca_8   ...       pca_44    pca_45    pca_46  \\\n",
       "0  0.201813 -0.035828 -0.104780   ...    -0.081093 -0.036612 -0.032685   \n",
       "1  0.015529 -0.417157 -0.044583   ...     0.008607  0.012202 -0.012626   \n",
       "2 -0.234224 -0.405027  0.267705   ...     0.035690 -0.022022 -0.055516   \n",
       "3  0.020324 -0.282483 -0.057967   ...     0.123055 -0.091573 -0.005661   \n",
       "4  0.056367  0.039180 -0.224581   ...    -0.030246  0.356076  0.198616   \n",
       "\n",
       "     pca_47    pca_48    pca_49    pca_50    pca_51    pca_52   target  \n",
       "0  0.026958  0.023477 -0.031667 -0.021681  0.014094  0.040825  Class_2  \n",
       "1 -0.053581  0.087355  0.009804 -0.011665 -0.019390  0.047540  Class_2  \n",
       "2 -0.078802 -0.089903  0.081683 -0.128651 -0.144019  0.000786  Class_2  \n",
       "3 -0.027059  0.032352  0.083729  0.069337  0.025704 -0.095524  Class_2  \n",
       "4  0.256928 -0.029483 -0.098445 -0.002309  0.016936 -0.085946  Class_8  \n",
       "\n",
       "[5 rows x 55 columns]"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_pca.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T04:44:00.091878Z",
     "start_time": "2020-07-17T04:44:00.084898Z"
    }
   },
   "outputs": [],
   "source": [
    "y_train_pca = train['target']\n",
    "x_train_pca = train.drop([\"id\", \"target\"], axis = 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 用降维前的数据训练RBF核SVM"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "由于在第3题中只对其中10000个样本降了维，要对比降维前和降维后的差异，就要保证训练样本是这一万个样本，所以采用交叉验证来调优"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T05:07:08.279816Z",
     "start_time": "2020-07-17T05:07:08.276809Z"
    }
   },
   "outputs": [],
   "source": [
    "from sklearn.svm import SVC\n",
    "from sklearn.model_selection import GridSearchCV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T05:08:15.731596Z",
     "start_time": "2020-07-17T05:08:15.728643Z"
    }
   },
   "outputs": [],
   "source": [
    "#设置超参数C_s和gamma_s\n",
    "C_s = np.logspace(-1, 3, 5)\n",
    "gamma_s = np.logspace(-1, 1, 3)\n",
    "params_gird = dict(gamma = gamma_s, C = C_s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T05:21:34.876661Z",
     "start_time": "2020-07-17T05:18:49.716871Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, error_score='raise-deprecating',\n",
       "       estimator=SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n",
       "  decision_function_shape='ovr', degree=3, gamma='auto_deprecated',\n",
       "  kernel='rbf', max_iter=-1, probability=False, random_state=None,\n",
       "  shrinking=True, tol=0.001, verbose=False),\n",
       "       fit_params=None, iid='warn', n_jobs=-1,\n",
       "       param_grid={'gamma': array([  0.1,   1. ,  10. ]), 'C': array([  1.00000e-01,   1.00000e+00,   1.00000e+01,   1.00000e+02,\n",
       "         1.00000e+03])},\n",
       "       pre_dispatch='2*n_jobs', refit=True, return_train_score=True,\n",
       "       scoring='accuracy', verbose=0)"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#生成学习器\n",
    "SVC1 = SVC(kernel = 'rbf')\n",
    "\n",
    "#生成GridSearchCV的实例（参数设置）\n",
    "grid_accuracy = GridSearchCV(SVC1, params_gird, cv = 5, scoring = 'accuracy', n_jobs = -1, return_train_score = True)\n",
    "\n",
    "#调用GridSearchCV的fit方法\n",
    "grid_accuracy.fit(x_train_part, y_train_part)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T05:21:48.359640Z",
     "start_time": "2020-07-17T05:21:48.354979Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.776125\n",
      "{'C': 10.0, 'gamma': 1.0}\n"
     ]
    }
   ],
   "source": [
    "#输出结果\n",
    "print( grid_accuracy.best_score_)\n",
    "print(grid_accuracy.best_params_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 用降维后的数据训练RBF核SVM\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T05:36:57.482022Z",
     "start_time": "2020-07-17T05:32:15.509188Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, error_score='raise-deprecating',\n",
       "       estimator=SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n",
       "  decision_function_shape='ovr', degree=3, gamma='auto_deprecated',\n",
       "  kernel='rbf', max_iter=-1, probability=False, random_state=None,\n",
       "  shrinking=True, tol=0.001, verbose=False),\n",
       "       fit_params=None, iid='warn', n_jobs=-1,\n",
       "       param_grid={'gamma': array([  0.1,   1. ,  10. ]), 'C': array([  1.00000e-01,   1.00000e+00,   1.00000e+01,   1.00000e+02,\n",
       "         1.00000e+03])},\n",
       "       pre_dispatch='2*n_jobs', refit=True, return_train_score=True,\n",
       "       scoring='accuracy', verbose=0)"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#生成学习器\n",
    "SVC2 = SVC(kernel = 'rbf')\n",
    "\n",
    "#生成GridSearchCV的实例（参数设置）\n",
    "grid_accuracy = GridSearchCV(SVC2, params_gird, cv = 5, scoring = 'accuracy', n_jobs = -1, return_train_score = True)\n",
    "\n",
    "#调用GridSearchCV的fit方法\n",
    "grid_accuracy.fit(x_train_pca, y_train_pca)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-07-17T05:38:06.943717Z",
     "start_time": "2020-07-17T05:38:06.938727Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7793\n",
      "{'C': 10.0, 'gamma': 1.0}\n"
     ]
    }
   ],
   "source": [
    "#输出结果\n",
    "print( grid_accuracy.best_score_)\n",
    "print(grid_accuracy.best_params_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过上面的降维前和降维后训练结果对比，差别不是很大，说明PCA降维起到了很好的作用，很多特征确实存在维度的冗余，通过降维降维去掉多余的维度很大程度上降低了计算的复杂度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "nbTranslate": {
   "displayLangs": [
    "*"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "en",
   "targetLang": "fr",
   "useGoogleTranslate": true
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
